<?php
// +----------------------------------------------------------------------
// | Author: wyd 2025/10/13
// | DESC: 
// +----------------------------------------------------------------------

declare (strict_types=1);

/*************************************************************************************************************/
//短名调用，和别名调用只能2选1，使用注解不能要参数
//$user = \My\core\BeanFactory::getBean('User');
//var_dump('11111');
//var_dump($user->name);

use Swoole\Coroutine\WaitGroup;
use Swoole\Runtime;

error_reporting(E_ALL);
ini_set('display_errors', '1');


//若某个类明明存在却提示not found  执行 composer dump-auto
require_once __DIR__ . '/../vendor/autoload.php';

require_once __DIR__ . '/app/config/define.php';

//这里让后续非协程代码开启协程
Runtime::enableCoroutine();

\Swoole\Coroutine\run(function () {
    $pool = new \My\core\pool\PdoPool();
    $pool->init();
    $wg = new WaitGroup();
    for ($i = 0; $i < 10; $i++) {
        $wg->add();
        go(function () use ($pool, $wg) {
            echo "开始查询1\n";
            /** @var PDO $conn */
            $conn = $pool->getConnection();

            defer(function () use ($pool, $conn, $wg) {
                $pool->close($conn);
                $wg->done();
            });
            $res = $conn->query("select sleep(5)");
            $res->setFetchMode(PDO::FETCH_ASSOC);
            $rows = $res->fetchAll();
            echo "查询1完成\n";

        });
    }

    for ($i = 0; $i < 5; $i++) {
        $wg->add();
        go(function () use ($pool, $i, $wg) {
            echo "开始查询2\n";
            /** @var PDO $conn */
            $conn = $pool->getConnection();
            defer(function () use ($pool, $conn, $wg) {
                $pool->close($conn);
                $wg->done();
            });
            $res = $conn->query("select {$i}");
            $res->setFetchMode(PDO::FETCH_ASSOC);
            $rows = $res->fetchAll();
           // var_dump($rows);
        });
    }
    echo $pool->getConnNum();
    $wg->wait();


    //一直执行
    while (true) {
        \Swoole\Coroutine::sleep(1);
    }
});

/**
 *
 * 开始查询1
 * 开始查询2
 * 查询1完成
 * 查询2完成
 * 设置 Runtime::enableCoroutine()后结果如下
 * 开始查询1
 * 开始查询2
 * 查询2完成
 * 查询1完成
 */


/*************************************************************************************************************/
//\My\core\BeanFactory::init();
//$dispatcher = \My\core\BeanFactory::getBean(\My\core\frame\RouterCollector::class)->getDispatcher();
//
//$routeInfo = $dispatcher->dispatch('GET', '/test/12321');
//switch ($routeInfo[0]) {
//    case FastRoute\Dispatcher::NOT_FOUND:
//        var_dump('404');
//        break;
//    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
//        $allowedMethods = $routeInfo[1];
//        var_dump('405');
//        // ... 405 Method Not Allowed
//        break;
//    case FastRoute\Dispatcher::FOUND:
//        $handler = $routeInfo[1];
//        $vars = $routeInfo[2];
//        $handler($vars,$my);
//        break;
//}
//
//die;



